﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BOMS.DAL;
using BOMS.Utility;

namespace BOMS.WebUI.Web.Warning
{
    public partial class warningProcessDetail : System.Web.UI.Page
    {
        public int WarningID
        {
            get { return int.Parse(Request.QueryString["id"]); }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bindData();
            }
        }
        private  void bindData( )
        {
            
            //得到报警实体
            Model.S_Warning Warn = (new BLL.S_Warning()).GetModel(WarningID);

            //得到与之关联的（电池或电池组）日志记录实体
            BLL.S_BatSubLog bll = new BLL.S_BatSubLog();
            object obj = bll.GetNearlyModel(
               Warn.WarningTime, Common.GENRATION_NUM, Warn.EquipID, Warn.BatteryGroup, Warn.BatteryID);

            string Warningstr;
            if (Warn.BatteryID == -1)
            {
                Warningstr = ((GroupErrorStatus)Common.WarningNOLogicalDealwith<GroupErrorStatus>(Warn.WarningNO)).ToString();
            }
            else
            {
                Warningstr = ((BatteryErrorStatus)Common.WarningNOLogicalDealwith<BatteryErrorStatus>(Warn.WarningNO)).ToString();
            }

            string WarningInfo;
            object[] parms = new object[]{
                        Warn.WarningTime.ToString("yyyy-MM-dd HH:mm:ss")
                        , Warn.HostName
                        , Warn.BatteryGroup
                        , Warn.BatteryID
                        , Warningstr
                };
            if (Warn.BatteryID != 0 && Warn.BatteryID != -1)//电池
            {
                WarningInfo = string.Format("<b>{0}</b>现场主机：<b>{1}</b>电池组：<b>{2}</b>电池：<b>{3}</b>异常：<b>{4}</b>", parms);
                ltrCaption.Text = "电池详情：";

                Model.S_BatSubLog Bat = obj as Model.S_BatSubLog;
                if (Bat != null)
                {
                    string BatInfo = string.Format("电压：<b>{0}V</b>内阻：<b>{1}mΩ</b>温度：<b>{2}℃</b>剩余容量：<b>{3}Ah</b>", Bat.Vval, Bat.Rval, Bat.Tval, Bat.Capacity);
                    ltrBatInfo.Text = BatInfo;
                }
            }
            else //电池组
            {
                WarningInfo = string.Format("<b>{0}</b>现场主机：<b>{1}</b>电池组：<b>{2}</b>异常：<b>{4}</b>", parms);
                ltrCaption.Text = "电池组详情：";

                Model.S_BatgrpSubLog Grp = obj as Model.S_BatgrpSubLog;
                if (Grp != null)
                {
                    //string BatGrpInfo = string.Format("总电压：<b>{0}V</b>总电流：<b>{1}A</b>剩余容量：<b>{2}Ah</b>剩余可用时：<b>{3}</b>工作状态：<b>{4}</b>", Grp.Vval, Grp.Ival, Grp.CapFree,Grp.TimeFree, Grp.Status);
                    string BatGrpInfo = string.Format("总电压：<b>{0}V</b>总电流：<b>{1}A</b>剩余容量：<b>{2}Ah</b>剩余可用时：<b>{3}</b>", Grp.Vval, Grp.Ival, Grp.CapFree, Grp.TimeFree);
                    ltrBatInfo.Text = BatGrpInfo;
                }
            }
            ltrWarningInfo.Text = WarningInfo;

            DataTable dt = WarningHistory.getWarningProcessInfo(WarningID);
            if (dt.Rows.Count > 0)
            {
                ltrOperator.Text = dt.Rows[0]["operator"].ToString();
                ltrCreate.Text = dt.Rows[0]["name"].ToString();
                ltrProcessDate.Text = dt.Rows[0]["createtime"].ToString();
                this.txtOperateDescr.InnerText = dt.Rows[0]["operatedescr"].ToString();
            }
        }
    }
}